import Constant from 'common/constant'
/**
 * 判断是否为空
 * @method isEmpty
 * @param  val
 * @return {boolean}
 */
const isEmpty = function (val) {
  return val === null || val === undefined || val === '' || val.length === 0 || JSON.stringify(val) === '{}'
}

/**
 * option对象属性的深拷贝
 */
const extendObject = (initalObj, finalObj) => {
  if (isEmpty(finalObj)) {
    return initalObj
  } else {
    for (let i in initalObj) {
      if (finalObj[i] === undefined) {
        finalObj[i] = initalObj[i]
      } else if (typeof initalObj[i] === 'object') {
        extendObject(initalObj[i], finalObj[i])
      }
    }
    return finalObj
  }
}

/**
 * 当前日期格式化
 * newDate >> yyyyMMdd
 */
const formatNowDate = function (strDate) {
  let date = ''
  if (typeof strDate === 'undefined') {
    date = new Date()
  } else {
    date = new Date(strDate)
  }
  var month = date.getMonth() + 1
  var day = date.getDate()
  month = month < 10 ? ('0' + month) : ('' + month)
  day = day < 10 ? ('0' + day) : ('' + day)
  return Number(date.getFullYear() + '' + month + day)
}

/**
 * 四舍五入
 * @param number 需要处理的数字
 * @param digits 小数位数
 */
const toFixed = function (number, digits) {
  if (number === '') {
    return ''
  }
  if (isNaN(number)) {
    return number
  }
  let object = Number(number)
  let result = object.toFixed(digits)

  if (result === '-0.00') { // 解决部分负数保留两位小数后，-0.00，0.00 页面样式不一致问题，如 -0.0000012
    result = '0.00'
  }

  return result
}

  /**
   * 工具类 获取url 参数
   * @method getUrlParam
   * @private
   */
const getUrlParam = (name) => {
  let url = decodeURI(location.search)
  let theRequest = {}
  if (url.indexOf('?') !== -1) {
    let str = url.substr(1)
    let strs = str.split('&')
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
    }
  }
  return theRequest[name]
}

/**
 *往本地存储token
 *要是header中有token，存入本地
 * @param  data 返回的请求头
*/
const saveyyzToken = (data) => {
  if ('gmas_token' in data) {
    sessionStorage.setItem(Constant.YYZ_TOKEN, data.gmas_token)
  }
}

/**
 *如果本地缓存中有token，就放入传送的参数中
 * @param param
*/
const setToken = function (param) {
  let token = sessionStorage.getItem(Constant.YYZ_TOKEN)
  if (token !== '' && token !== null) {
    param.gmas_token = token
  }
  return param
}

/**
 *处理金策略的游客身份也可以查看的接口
 * 30012,30016,30008,30006,30010,30014,30026
 * @param param
 * @param bridgeParam
 */
const setTouristParam = function (param, bridgeParam) {
  let usercode = sessionStorage.getItem(Constant.storage.USER_CODE)
  if (usercode === '' || usercode === null || usercode === undefined) {
    // 游客状态
    bridgeParam.param = param
    bridgeParam.touristParameter = {
      'mobile': 'phone_no',
      'password': 'stock_pwd',
      'user_code': 'user_code'
    }
  } else {
    // 登录状态
    param.user_code = usercode
    // param.client_id = usercode
    bridgeParam.param = param
    bridgeParam.localParameter = {'mobile': 'phone_no', 'password': 'stock_pwd'}
  }
  return bridgeParam
}

/**
 *处理用户中心的游客身份也可以查看的接口
 * 12008301,12008302,12008305,12008310,12008312,12008315,12008316
 * @param param
 * @param bridgeParam
*/
const setUsercenterTouristParam = function (param, bridgeParam) {
  let usercode = sessionStorage.getItem(Constant.storage.USER_CODE)
  if (usercode === '' || usercode === null || usercode === undefined) {
    // 游客状态
    bridgeParam.param = param
    bridgeParam.touristParameter = {'mobile': 'phone_no', 'password': 'stock_pwd', 'user_code': 'user_code'}
  } else {
    // 登录状态
    param.user_code = usercode
    bridgeParam.param = param
    bridgeParam.localParameter = {'mobile': 'phone_no', 'password': 'stock_pwd'}
  }
  return bridgeParam
}

export {
  isEmpty,
  extendObject,
  formatNowDate,
  toFixed,
  getUrlParam,
  saveyyzToken,
  setToken,
  setTouristParam,
  setUsercenterTouristParam
}
